package canss.supports;

public class Makequery_select
{
	private String target;
	private String from;
	private String where;
	private String orderby;
	private String groupby;
	private String limit;

	public Makequery_select Select(String _target)
	{
		if(basic.isEmpty(target))
			target = _target;
		else
			target += ","+_target;
		return this;
	}

	public Makequery_select PushFrom(String _from)
	{
		if(basic.isEmpty(from) && basic.isNotEmpty(_from))
			from = String.format("%s", _from);
		else if(basic.isNotEmpty(_from))
			from += String.format(" , %s", _from);
		return this;
	}

	public Makequery_select PushWhere(String _where)
	{
		if(basic.isEmpty(where) && basic.isNotEmpty(_where))
			where = String.format("(%s)", _where);
		else if(basic.isNotEmpty(_where))
			where += String.format(" AND (%s)", _where);
		return this;
	}

	public Makequery_select OrderBy(String _orderby)
	{
		if(basic.isEmpty(orderby))
			orderby = _orderby;
		else
			orderby += ","+_orderby;
		return this;
	}

	public Makequery_select GroupBy(String _groupby)
	{
		groupby = _groupby;
		return this;
	}

	public Makequery_select Limit(String _limit)
	{
		limit = _limit;
		return this;
	}

	public String[] getColNamesAlias()
	{
		if (basic.isNotEmpty(target))
		{
			String[] tmp = target.split(",");
			for (int i = 0; i < tmp.length; i++)
			{
				int pos = 0;
				if ((pos = tmp[i].toLowerCase().indexOf(" as ")) >= 0)
					tmp[i] = tmp[i].substring(pos + " as ".length()).trim();
				else
					tmp[i] = tmp[i].trim();
			}
			return tmp;
		}
		return null;
	}
	public String[] getColNames()
	{
		if (basic.isNotEmpty(target))
		{
			String[] tmp = target.split(",");
			for (int i = 0; i < tmp.length; i++)
			{
				int pos = 0;
				if ((pos = tmp[i].toLowerCase().indexOf(" as ")) >= 0)
					tmp[i] = tmp[i].substring(0, pos).trim();
				else
					tmp[i] = tmp[i].trim();
			}
			return tmp;
		}
		return null;
	}
	public String NameAlias2Origin(String _str)
	{
		if(basic.isNotEmpty(_str))
		{
			String tmp = _str.trim();
			String[] alias = this.getColNamesAlias();
			String[] origin = this.getColNames();
			for(int i=0; i<alias.length; i++)
				if(tmp.equalsIgnoreCase(alias[i]))
					return origin[i];
		}
		return null;
	}

	public String toString(boolean _assubquery, String _deli)
	{
		if(_assubquery)
			return String.format(" (%s) %s ", this.toString(), _deli);
		return this.toString();
	}
	@Override
	public String toString()
	{
		if (basic.isEmpty(target, from))
			return "";

		StringBuffer query = new StringBuffer();
		query.append(String.format("SELECT %s FROM (%s) ", target, from));
		if (basic.isNotEmpty(where))
			query.append(String.format("WHERE (%s) ", where));
		if (basic.isNotEmpty(groupby))
			query.append(String.format("GROUP BY %s ", groupby));
		if (basic.isNotEmpty(orderby))
			query.append(String.format("ORDER BY %s ", orderby));
		if (basic.isNotEmpty(limit))
			query.append(String.format("LIMIT %s ", limit));

		return query.toString();
	}

	public void reset()
	{
		target = null;
		from = null;
		where = null;
		orderby = null;
		groupby = null;
		limit = null;
	}
}
